﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SeriesThequeMVVM.Windows
{

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;

    
    public abstract class ViewModel : ObservableObject, IDataErrorInfo
    {
        
        public string this[string columnName]
        {
            get { return OnValidate(columnName); }
        }

        
        [Obsolete]
        public string Error
        {
            get { throw new NotSupportedException(); }
        }

        
        protected virtual string OnValidate(string propertyName)
        {
            var context = new ValidationContext(this)
                          {
                              MemberName = propertyName
                             
                          };

            var results  = new Collection<ValidationResult>();
            bool isValid = Validator.TryValidateObject(this, context, results, true);

            return !isValid ? results[0].ErrorMessage : null;
        }
    }
}

